package com.browsesoft.util;

import java.util.Iterator;
import java.util.LinkedList;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.user.User;

public class SealsHelper {

	/**
	 * 将委托人的印章权限授予被委托人
	 * 
	 */
	public static void SealHandover(User weituoren, User beiweituoren) {
		// 得到委托人id,被委托人id
		String weituoID = weituoren.getID();
		String beiweituoID = beiweituoren.getID();
		// 给被委托人添加印章权限
		LinkedList seals = getSeals(weituoID);
		Iterator iter = seals.iterator();
		while (iter.hasNext()) {
			Entity se = (Entity) iter.next();
			String sealID = (String) se.getAttributes().get("csealid");
			// 如果aip_user_seal中没有该数据
			if (!hasData(sealID, beiweituoID)) {
				// 插入数据
				insertUserSealData(sealID, beiweituoID);
			}
		}
	}

	/**
	 * 插入印章数据
	 */
	private static void insertUserSealData(String sealID, String userID) {
		String sql = "insert into aip_user_seal values('" + sealID + "','"
				+ userID + "')";
		try {
			DBTools.excuteUpdate(sql);
		} catch (Exception e) {
			throw new RuntimeException("插入委托印章数据失败", e);
		}
	}

	/**
	 * 判断是否有改委托印章数据
	 */
	private static boolean hasData(String sealID, String userID) {
		try {
			String sql = "select * from aip_user_seal where " + "csealid='"
					+ sealID + "' and cuserid = '" + userID + "'";
			String[][] datas = DBTools.executeQueryWithTableHead(sql);
			if (datas.length > 1) {
				return true;
			}
			return false;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 找到给定拥护的所有印章
	 */
	public static LinkedList getSeals(String userid) {
		try {
			return EntityManager.getInstance().getObjects(
					"com.browsesoft.docdispose.SealEntity", "userid", userid);
		} catch (Exception e) {
			return new LinkedList();
		}
	}

	/**
	 * 清除委托印章授权数据
	 */
	public static void deleteTempWeituoUserSealData(User weituoren,
			User beiweituoren) {
		try {
			if (DBTools.isTableExist("t_wt")) {
				String weituoName = (String) weituoren.getAttributes().get(
						"name");
				String beiweituoName = (String) beiweituoren.getAttributes()
						.get("name");
				// 如果没有对应的委托信息
				String sql = "select f_shiwu,f_peopleinvited,weituouser from t_wt where f_state='正在委托'"
						+ " and weituouser = '"
						+ weituoName
						+ "' and f_peopleinvited = '" + beiweituoName + "'";
				String[][] datas = DBTools.executeQueryWithTableHead(sql);
				if (datas.length == 1) {
					deleteWeituoUserSealData(weituoren, beiweituoren);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("查询委托信息失败", e);
		}

	}

	/**
	 * 清楚委托数据
	 * 
	 */
	public static void deleteWeituoUserSealData(User weituoren,
			User beiweituoren) {
		try {
			// 找到委托人的印章id, 删除被委托人的印章授权信息
			String userID = beiweituoren.getID();
			LinkedList list = getSeals(weituoren.getID());
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Entity se = (Entity) iter.next();
				String sealID = (String) se.getAttributes().get("csealid");
				String sql = " delete from aip_user_seal where " + "csealid='"
						+ sealID + "' and cuserid = '" + userID + "'";

				DBTools.excuteUpdate(sql);
			}
		} catch (Exception e) {
			throw new RuntimeException("删除印章数据失败", e);
		}
	}
}
